//
//  MPTabBar.swift
//  Whiteboard
//
//  Created by point on 2018/1/4.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit

class MPTabBar: UITabBar {
    
    var centerBtn:UIButton
    
    override init(frame: CGRect) {
        self.centerBtn =  UIButton.createButton(imageStr: kIcon_record, imageColor: kColorDarkSlateGray, selected: kIcon_recordfill, imageselectedColor: kColorOrange, size: CGSize(width: 64, height: 64))
        
        self.centerBtn.bounds = CGRect(x: 0, y: 0, width: 64, height: 64)
        super.init(frame: frame)
        addSubview(self.centerBtn)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.centerBtn.center = CGPoint(x: self.bounds.size.width * 0.5, y:  self.bounds.size.height * 0.3 )
        
        
        var index:CGFloat = 0.0
        let width = self.bounds.size.width / 5;
        for subView in self.subviews {
            let className: AnyClass = subView.classForCoder
            guard let classNameStr  = NSString(cString: class_getName(className), encoding: String.Encoding.utf8.rawValue) else {
                return
            }
            if classNameStr.isEqual(to: "UITabBarButton") {
                
                
                subView.frame = CGRect(x: index * width, y: self.bounds.origin.y, width: width, height: self.bounds.size.height - 5)
                index = index + 1;
                
                if (index == 2) {
                    index = index + 1;
                }
            }
        }
    }
    
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if isHidden == false {
            let newPoint: CGPoint = convert(point, to: centerBtn)
            if centerBtn.point(inside: newPoint, with: event) {
                return centerBtn
            }
            else {
                return super.hitTest(point, with: event)
            }
        }
        else {
            return super.hitTest(point, with: event)
        }
    }
    
}
